/*
 *   Copyright 1999-2016 Asiainfo Technologies(China),Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.asiainfo.redis.web.controller;

import com.asiainfo.redis.entity.Result;
import com.asiainfo.redis.web.DateTypeEditor;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.ClusterStateFailureException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;

/**
 * @version v 1.0 on 2016/3/3 14:23
 * @auther william.xu
 */
public class BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new DateTypeEditor());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        Result result = new Result();
//        if (e instanceof org.springframework.beans.TypeMismatchException || e instanceof org.springframework
// .validation.BindException){
//            assembleFalureResult(result, ErrorCodeEnum.PARAMETER_FORMAT_ERROR);
//        }else if (e instanceof org.springframework.web.bind.MissingServletRequestParameterException){
//            assembleFalureResult(result, ErrorCodeEnum.PARAMETER_VALID_NOT_NULL);
//        }else if (e instanceof ServiceException){
//            assembleFalureResult(result, (ServiceException)e);
//        }else{
//            assembleFalureResult(result, ErrorCodeEnum.SYSTEM_ERROR);
//        }
        result.setSuccess(false);
        if (e instanceof ClusterStateFailureException) {
            result.setErrorMsg("该Redis集群不可用！");
        } else {
            result.setErrorMsg(e.getMessage());
        }
        LOGGER.error("执行出现错误：", e);
        return result;
    }

    protected Map encodedString2Map(String value) throws Exception{
        String origialValue = URLDecoder.decode(value, "UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true) ;
        //Map data = mapper.readValue(origialValue, Map.class);
        Map data = mapper.readValue(origialValue, new TypeReference<Map>() {});
        return data;
    }


}
